package at.dl.android.dota.adviser;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import at.dl.android.dota.adviser.R;

public class ShowHeroActivity extends ActivityWithDB {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_hero);
        
        final Bundle extras = getIntent().getExtras();
        String name1 = extras.getString(ListHeroActivity.HERO_NAME1);

        TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
        tabHost.setup();
        TabSpec spec1 = tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.infoLL);
        spec1.setIndicator("General",getResources().getDrawable(R.drawable.tab_stats));
        TabSpec spec2 = tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Skillorder",getResources().getDrawable(R.drawable.tab_lagunablade));
        spec2.setContent(R.id.skillLL);
        TabSpec spec3 = tabHost.newTabSpec("Tab 3");
        spec3.setIndicator("Itembuild",getResources().getDrawable(R.drawable.tab_broadsword));
        spec3.setContent(R.id.itemLL);
        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);

        SQLiteDatabase db = getDataBaseHelper().getReadableDatabase();
        String qry = "SELECT * FROM heroes WHERE name1 = \"" + name1 + "\"";
        Cursor cursor = db.rawQuery(qry,new String[0]);
        cursor.moveToFirst();

        setHead(name1,cursor);

        String roles = getEnumString(cursor,
                                     ColumnNames.Heroes.carry,
                                     ColumnNames.Heroes.semi_carry,
                                     ColumnNames.Heroes.ganker,
                                     ColumnNames.Heroes.hunter,
                                     ColumnNames.Heroes.supporter,
                                     ColumnNames.Heroes.tank,
                                     ColumnNames.Heroes.ward_bitch,
                                     ColumnNames.Heroes.disabler,
                                     ColumnNames.Heroes.initiator,
                                     ColumnNames.Heroes.pusher,
                                     ColumnNames.Heroes.anti_pusher);
        TextView t = (TextView) findViewById(R.id.roles);
        t.setText(roles);

        String lanes = getEnumString(cursor,
                                     ColumnNames.Heroes.solo,
                                     ColumnNames.Heroes.dual,
                                     ColumnNames.Heroes.tri,
                                     ColumnNames.Heroes.jungle);
        t = (TextView) findViewById(R.id.lanes);
        t.setText(lanes);
        
        String restrictions = getEnumString(cursor,
                                            ColumnNames.Heroes.orb_effect,
                                            ColumnNames.Heroes.buff_placer);
        if ( restrictions.trim().length() < 1 )
            restrictions = getResources().getString(R.string.non);
        t = (TextView) findViewById(R.id.restrictions);
        t.setText(restrictions);

        String features = getEnumString(cursor,
                ColumnNames.Heroes.bash,
                ColumnNames.Heroes.stun,
                ColumnNames.Heroes.aoe_stun,
                ColumnNames.Heroes.mini_stun,
                ColumnNames.Heroes.slow,
                ColumnNames.Heroes.aoe_slow,
                ColumnNames.Heroes.hold,
                ColumnNames.Heroes.aoe_hold,
                ColumnNames.Heroes.hex,
                ColumnNames.Heroes.silence,
                ColumnNames.Heroes.aoe_silence,
                ColumnNames.Heroes.blind,
                ColumnNames.Heroes.aoe_blind,
                ColumnNames.Heroes.aoe_dmg,
                ColumnNames.Heroes.dot,
                ColumnNames.Heroes.nuke,
                ColumnNames.Heroes.heal,
                ColumnNames.Heroes.evasion,
                ColumnNames.Heroes.protection,
                ColumnNames.Heroes.extended_dmg,
                ColumnNames.Heroes.crit,
                ColumnNames.Heroes.own_images,
                ColumnNames.Heroes.other_images,
                ColumnNames.Heroes.summons,
                ColumnNames.Heroes.take_control,
                ColumnNames.Heroes.blink,
                ColumnNames.Heroes.windwalk,
                ColumnNames.Heroes.manaburn,
                ColumnNames.Heroes.tracking,
                ColumnNames.Heroes.minus_armor,
                ColumnNames.Heroes.invisibility,
                ColumnNames.Heroes.bring_back,
                ColumnNames.Heroes.purge,
                ColumnNames.Heroes.escape);
        t = (TextView) findViewById(R.id.features);
        t.setText(features);

        setLinks(cursor);
        
        setSkills(cursor);
        
        setItems(cursor);
    }

    private void setLinks ( Cursor cursor ) {
        setLinkButton(cursor,ColumnNames.Heroes.link1_text,
                      ColumnNames.Heroes.link1_url,R.id.button_link_1);
        setLinkButton(cursor,ColumnNames.Heroes.link2_text,
                      ColumnNames.Heroes.link2_url,R.id.button_link_2);
    }
    
    private void setLinkButton ( Cursor cursor, String textColName, final String urlColName, int buttonId ) {
        String text = cursor.getString(cursor.getColumnIndex(textColName));
        final String url = cursor.getString(cursor.getColumnIndex(urlColName));
        Button t = (Button) findViewById(buttonId);
        t.setText(text);
        t.setOnClickListener(new OnClickListener(){
            public void onClick ( View v ) {
                Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
                startActivity(intent);
            }
        });
    }

    private void setHead ( String name1, Cursor cursor ) {
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageResource(cursor.getInt(cursor.getColumnIndex(ColumnNames.Heroes.hero_image_id)));
        TextView t = (TextView) findViewById(R.id.name1);
        t.setText(name1);
        t = (TextView) findViewById(R.id.name2);
        
        t.setText(cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.name2)));
        t = (TextView) findViewById(R.id.primattr);
        t.setText(cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.primaty_att)));
        t = (TextView) findViewById(R.id.party);
        t.setText(cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.party)));
        t = (TextView) findViewById(R.id.range);
        t.setText(cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.range)));
        t = (TextView) findViewById(R.id.ms);
        t.setText(cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.movement_speed)));
    }

    private void setSkills ( Cursor cursor ) {
        int[] imgIDs = new int[5];
        imgIDs[0] = R.drawable.stats;
        imgIDs[1] = cursor.getInt(cursor.getColumnIndex(ColumnNames.Heroes.skill1_image_id)); 
        imgIDs[2] = cursor.getInt(cursor.getColumnIndex(ColumnNames.Heroes.skill2_image_id)); 
        imgIDs[3] = cursor.getInt(cursor.getColumnIndex(ColumnNames.Heroes.skill3_image_id)); 
        imgIDs[4] = cursor.getInt(cursor.getColumnIndex(ColumnNames.Heroes.skill4_image_id));
        String[] skillNames = new String[5];
        skillNames[0] = "Stats";
        skillNames[1] = cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.skill1));
        skillNames[2] = cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.skill2));
        skillNames[3] = cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.skill3));
        skillNames[4] = cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.skill4));
        String skillingStr = cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.skillorder));
        char[] chars = new char[skillingStr.length()];
        skillingStr.getChars(0,skillingStr.length(),chars,0);
        int l = chars.length;
        int[] ids = new int[chars.length];
        String[] names = new String[chars.length];
        for ( int i = 0; i < l; i++ ) {
            int numSkill = chars[i] - '0';
            ids[i] = imgIDs[numSkill];
            names[i] = skillNames[numSkill];
        }
        GridView skillGridView = (GridView) findViewById(R.id.skills);
        SimpleImageAdapter sia = new SimpleImageAdapter(this,ids,names);
        skillGridView.setAdapter(sia);
    }
    
    private void setItems ( Cursor cursor ) {
        String[] itemNames = new String[ColumnNames.Heroes._item_count];  
        int[] itemIDs = new int[ColumnNames.Heroes._item_count];
        for ( int i = 1; i <= ColumnNames.Heroes._item_count; i++ ) {
            itemNames[i-1] = cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes._item_base_name + i));
            itemIDs[i-1] = cursor.getInt(cursor.getColumnIndex(ColumnNames.Heroes._item_base_name + i + ColumnNames.Heroes._item_image_id_tail));
        }
        GridView itemGridView = (GridView) findViewById(R.id.items);
        CallActivityImageAdapter sia = new CallActivityImageAdapter(this,ShowItemActivity.class,ShowItemActivity.ITEM_NAME,itemIDs,itemNames);
        itemGridView.setAdapter(sia);
        
        String[] altItemNames = new String[ColumnNames.Heroes._alt_item_count];
        int[] altItemIDs = new int[ColumnNames.Heroes._alt_item_count];
        for ( int i = 1; i <= ColumnNames.Heroes._alt_item_count; i++ ) {
            altItemNames[i-1] = cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes._alt_item_base_name + i));
            altItemIDs[i-1] = cursor.getInt(cursor.getColumnIndex(ColumnNames.Heroes._alt_item_base_name + i + ColumnNames.Heroes._alt_item_image_id_tail));
        }
        itemGridView = (GridView) findViewById(R.id.alt_items);
        sia = new CallActivityImageAdapter(this,ShowItemActivity.class,ShowItemActivity.ITEM_NAME,altItemIDs,altItemNames);
        itemGridView.setAdapter(sia);
    }
    
    private String getEnumString ( Cursor cursor, String...fieldNames ) {
        StringBuilder sb = new StringBuilder(128);
        boolean first = true;
        for ( String fieldName : fieldNames ) {
            int ndx = cursor.getColumnIndex(fieldName);
            if ( ! cursor.isNull(ndx) && cursor.getInt(ndx) != 0 ) { 
                if ( first )
                    first = false;
                else
                    sb.append(", ");
                sb.append(beautifyName(fieldName));
            }
        }
        return sb.toString();
    }

    private String beautifyName ( String name ) {
        char[] chars = new char[name.length()];
        name.getChars(0,name.length(),chars,0);
        for ( int i = 0; i < chars.length; i++ ) {
            if ( chars[i] == '_' )
                chars[i] = '-';
            if ( i == 0 || chars[i-1] == '-' )
                chars[i] = Character.toUpperCase(chars[i]);
        }
        return new String(chars);
    }
    
}
